package com.jetpackframework.retrofit;

import com.blankj.utilcode.util.LogUtils;
import com.jetpackframework.http.HttpObserver;
import com.jetpackframework.rxjetpack.Dispose;
import com.jetpackframework.rxjetpack.Observable;
import com.jetpackframework.rxjetpack.Observer;
import com.jetpackframework.rxjetpack.thread.Schedules;
import com.jetpackframework.rxmvvm.BaseViewModel;

import java.util.Objects;

import okhttp3.Headers;
import okhttp3.WebSocketListener;

/**
 * Created by Administrator on 2018/1/25.
 *
 * Http 请求基类(动态代理模式)
 * 数据库操作基类
 */
public abstract class BaseDataPresenter<H,D,M> extends WebSocketListener implements HttpObserver<M> {
    private Retrofit retrofitOKHttp;
    private H httpApi;
    private D dbDao;
    private BaseViewModel viewModel;
    public BaseDataPresenter(){
        retrofitOKHttp = Retrofit.getInstance();
        if (retrofitOKHttp != null) {
            httpApi = retrofitOKHttp.create(getHttpPresenterClass());
            dbDao = retrofitOKHttp.create(getDbPresenterClass());
        }

    }

    protected abstract Class<D> getDbPresenterClass();

    protected abstract Class<H> getHttpPresenterClass();

    protected void addHttpSubscriber(Observable<M> observable){
        if (observable instanceof ObservableHttp){
            ((ObservableHttp<M>) observable).httpObserver(this).life(viewModel.owner.getLifecycle());
        }
        toSubscribe(observable);
    }
    protected void addDBSubscriber(Observable<M> observable){
        Objects.requireNonNull(observable);
        observable.observeOn(Schedules.androidMainThread())
                .subscribeOn(Schedules.androidMainThread())
                .subscribe(new Observer<M>() {
                    @Override
                    public void onSubscribe(Dispose dispose) {

                    }

                    @Override
                    public void onNext(M s) throws Exception {
                        onNextDatabaseResult(s);
                    }

                    @Override
                    public void onError(Throwable t) {
                        onErrorResult((Exception) t,0);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    protected void addWebSocketSubscriber(Observable<M> observable){
        if (observable instanceof ObservableHttp){
            ((ObservableHttp<M>) observable).webSocketListener(this);
        }
        toSubscribe(observable);
    }
    protected void addFtpSubscriber(Observable observable){
        toSubscribe(observable);
    }

    @Override
    public void onError(Exception e, int id) {
        onErrorResult(e,id);
        LogUtils.e(e);
    }

    @Override
    public void onNext(M response, int id) {
        onNextResult(response,id);
    }
    protected H getHttpPresenter(){
        return httpApi;
    }
    protected D getDbDao(){
        return dbDao;
    }
    private void toSubscribe(Observable<M> o) {
        o.observeOn(Schedules.io())
                .subscribeOn(Schedules.androidMainThread())
                .subscribe();
    }

    protected void onNextDatabaseResult(M s) {
    }

    @Override
    public void onHeader(Headers headers) {

    }

    protected abstract  void onNextResult(M response, int id);

    protected abstract void onErrorResult(Exception e, int id);

    protected BaseViewModel getViewModel() {
        return viewModel;
    }

    public void setViewModel(BaseViewModel viewModel) {
        this.viewModel = viewModel;
    }
    protected void runThreadRunnable(Runnable runnable){
        getViewModel().runnableInThread(runnable);
    }
}
